/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.component;

import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;


/**
 * Center Layout Container
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class DkCenterLayoutContainer extends SimpleContainer
{

	private int width;

	/**
	 * Constructor.
	 * 
	 * @param width
	 *           the width
	 */
	public DkCenterLayoutContainer(final int width)
	{
		this(width, null);
	}

	/**
	 * Constructor.
	 * 
	 * @param width
	 *           the width
	 * @param toCenter
	 *           the widget to be centered
	 */
	public DkCenterLayoutContainer(final int width, final IsWidget toCenter)
	{
		this.width = width;
		if (toCenter != null)
		{
			add(toCenter);
		}
	}

	@Override
	protected void doLayout()
	{
		super.doLayout();
		getElement().getStyle().setProperty("textAlign", "center"); // IE hack for center alignment
		getElement().addClassName("fill-this-background-color");
		if (getWidgetCount() > 0)
		{
			for (int i = 0; i < getWidgetCount(); i++)
			{
				final Widget widget = getWidget(i);
				widget.getElement().getStyle().setProperty("textAlign", "left"); // clear IE hack
				widget.getElement().getStyle().setProperty("marginTop", "0");
				widget.getElement().getStyle().setProperty("marginRight", "auto");
				widget.getElement().getStyle().setProperty("marginBottom", "0");
				widget.getElement().getStyle().setProperty("marginLeft", "auto");
				widget.getElement().getStyle().setPosition(Position.RELATIVE);
				widget.getElement().getStyle().setWidth(700, Unit.PX);
			}
		}
	}

	/**
	 * @return the width
	 */
	public int getWidth()
	{
		return width;
	}

	/**
	 * Cannot be changed after layout is applied
	 * 
	 * @param width
	 *           the width to set
	 */
	@Override
	public void setWidth(final int width)
	{
		this.width = width;
	}
}
